﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ManagerGame;
using ManagerGame.Screens;

namespace ManagerGame
{
    /// <summary>
    /// Implementation of the rules doing calculations at the end of the turn
    /// </summary>
    class RulesEngine
    {

        Boolean caught;
        public static double _totalRisk = 0;
        
        /// <summary>
        /// Process the end of turn calculations
        /// </summary>
        /// <param name="manager">Manager</param>
        public Boolean MakeTurn(Manager manager)
        {
            _totalRisk = 0;
            caught = false;
            if (manager.BusinessList != null)
            {
                foreach (Business bisnes in manager.BusinessList)
                {
                    CalculateValues(bisnes);
                }
                CalculateTotalIncome(manager);
                CalculatePoliceIntervention(manager);
                manager.TurnCounter++;                
            }
            return caught;
        }

        /// <summary>
        /// Iterates through managers business and resets their companies id counters.
        /// </summary>
        /// <param name="manager">Manager</param>
        public static void ResetCompanyIdCounters(Manager manager)
        {
            if (manager.BusinessList != null)
            {
                foreach (Business item in manager.BusinessList)
                {
                    if (item.Companies != null)
                    {
                        foreach (Company company in item.Companies)
                        {
                            company.ResetCounters();
                        }
                    }

                }
            }
        }

        /// <summary>
        /// Calculates total income for the manager
        /// </summary>
        /// <param name="manager">Manager</param>
        public void CalculateTotalIncome(Manager manager)
        {
            foreach (Business bisnes in manager.BusinessList)
            {
                manager.Money += bisnes.TotalIncome;
            }
        }

        /// <summary>
        /// Calculate new values for the manager's companies
        /// </summary>
        /// <param name="bisnes">Business from which the values are calculated</param>
        /// <returns>Manager at the start of a new turn</returns>
        public Business CalculateValues (Business bisnes)
        {
            bisnes.TotalRisk = 0;
            bisnes.TotalIncome = 0;

            foreach (Company company in bisnes.Companies)
            {
                Random _rand = new Random();
                double income;
                CalculateHappiness(company);
                income =
                    Math.Round(
                        (company.Income*(_rand.NextDouble() + 0.5))*((double) company.Happiness + company.Workers), 2);

                bisnes.TotalIncome += (income - (company.Salary*company.Workers));
                if (company.Happiness == Happiness.Happy)
                {
                    bisnes.TotalRisk += 0.2;
                    bisnes.TotalIncome -= 200;
                }
                if (company.Happiness == Happiness.NotThatHappy)
                    bisnes.TotalRisk -= 0.2;
                bisnes.TotalRisk += company.Risk;                    
            }

            return bisnes;
        }

        /// <summary>
        /// Calculates the possibility of police intervention for the manager, based on risk values of different businesses
        /// </summary>
        /// <param name="manager">Manager</param>
        public void CalculatePoliceIntervention(Manager manager) 
 
        {
            Random _rand = new Random();
            _totalRisk = 0;
            foreach (Business bisnes in manager.BusinessList)
            {
                _totalRisk += bisnes.TotalRisk;
            }

            double randomnvalue = ((_rand.NextDouble() + 0.4) * (-1));
            if (_totalRisk < randomnvalue)
            {
                caught = true;
            }
        }

        /// <summary>
        /// Calculate company happiness for this round
        /// </summary>
        /// <param name="company">Company</param>
        private void CalculateHappiness(Company company) {

            Random _rand = new Random();

            if (company.Operations != null && company.HaveSauna())
            {
                company.Happiness = Happiness.Happy;
            }
            else if (company.SalaryAmountIndex >= company.WorkerAmountIndex)
            {
                if (company.Happiness != Happiness.Happy && _rand.NextDouble() > 0.4)
                    company.Happiness = Happiness.Content;
            }
            else
            {
                company.Happiness = Happiness.NotThatHappy;

            }
        }

    }
}
